<?php

if (!defined('__BACKEND__'))
    die("you cannot directly access this page");
/*
 * by SONY K Wibisono
 * e-mail : sony@sonywibisono.com
 */

//Place code here

function insert_keyword($text){
    if (!empty($text)){
    $word=explode(" ", $text);
    for ($i=0;$i<count($word);$i++){
        if(!is_word_exist($word[$i])){
            $word_id[$i]=insert_word($word[$i]);    
        } else $word_id[$i]=get_word_id ($word[$i]);
    }
}
}
function insert_search_index($post_id,$word_id){
    global $db;
    $sql="insert into _search_index values ($post_id,$word_id)";
    $db->query($sql);
}
function insert_event_index($event_id,$word_id){
    global $db;
    $sql="insert into _event_index values ($event_id,$word_id)";
    $db->query($sql);
}
function is_index_exist ($word_id,$post_id){
    global $db;
    $sql="select count(*) from _search_index where word_id=$word_id and post_id=$post_id";
    $row=$db->fetch_row($sql);
    if (!empty($row[0])){
        return $row[0]>0;
    } else return FALSE;
}
function is_event_index_exist ($event_id,$word_id){
    global $db;
    $sql="select count(*) from _event_index where word_id=$word_id and event_id=$event_id";
    $row=$db->fetch_row($sql);
    if (!empty($row[0])){
        return $row[0]>0;
    } else return FALSE;
}
function indexing_process() {
    global $db;
    $sql="select * from _post_contents order by id asc";
    $result=$db->query($sql);
    while ($row=$db->fetch_array($result)){
        extract($row);
        $html = array("<p>","</p>","<i>","</i>","<b>","</b>","<", ">", ";", "&","=",",",".",'"',
            ':','/','(',')','0','1','2','3','4','5','6','7','8','9','-'); 
        $wordContents=str_replace($html, " ",$contents);
        $word=explode(" ", $wordContents);
        for ($i=0;$i<count($word);$i++){
        if(is_word_exist($word[$i])){
            $word_id[$i]=get_word_id ($word[$i]);        
        } else {
            $word_id[$i]=insert_word($word[$i]); 
        }
        if(!is_index_exist($word_id[$i], $id)){
        insert_search_index($id, $word_id[$i]);
        }
        } 
        
    }
    $sql="select * from _event_events order by id asc";
    $result=$db->query($sql);
    while ($row=$db->fetch_array($result)){
        extract($row);
        $html = array("<p>","</p>","<i>","</i>","<b>","</b>","<", ">", ";", "&","=",",",".",'"',
            ':','/','(',')','0','1','2','3','4','5','6','7','8','9','-'); 
        $wordContents=str_replace($html, " ",$event_content);
        $word=explode(" ", $wordContents);
        for ($i=0;$i<count($word);$i++){
        if(is_word_exist($word[$i])){
            $word_id[$i]=get_word_id ($word[$i]);        
        } else {
            $word_id[$i]=insert_word($word[$i]); 
        }
        if(!is_event_index_exist($id,$word_id[$i])){
        insert_event_index($id, $word_id[$i]);
        }
        } 
        
    }
}
function keyword_index(){
    echo "<form action=\"?mod=panel&file=keyword\" method=\"post\">
        <input type=\"hidden\" name=\"action\" value=\"indexing\"/>
        <input id=\"indexing\" type=submit value=\"Start Index\" />
        </form>";
}

function word_count(){
    global $db,$ws;
    if (empty($ws)){
        $sql="select count(*) as cnt from _keywords";
    } else {
        $sql="select count(*) as cnt from _keywords where word like '%$ws%'";
    }
    
    $row=$db->fetch_row($sql);
    return $row['cnt'];
}
function word_on_post($wordid){
    global $db,$ws;
    if (empty($ws)){
        $sql="select count(*) as cnt from _post_tags where word_id=$wordid";
    } else {
        $sql="select count(*) as cnt from _post_tags where word_id=$wordid";
    }
    $row=$db->fetch_row($sql);
    if (!empty($row)){
    return $row['cnt'];
    } else return 0;
}
function word_list(){
    global $db,$page,$pagesize,$ws;
    if (empty($page)){
        $start=0;
    } else {
        $start=$page*$pagesize;
    }
    if (empty($ws)){
        $sql="select a.*
          from _keywords a
          order by a.word asc 
           limit $start,$pagesize";
    } else {
        $sql="select a.*
          from _keywords a 
          where a.word like '%$ws%'
          order by a.word asc 
           limit $start,$pagesize";
    }
    
    $result=$db->query($sql);
    $nrow=mysql_num_rows($result);
    if ($nrow>0){
        $p=floor(word_count()/$pagesize);
        echo "<h2>List of Keyword</h2>
            <form id=\"keyword_form\" action=\"?mod=panel&file=keyword\" method='post'>Search : 
            
            <input type='text' name='ws'/>
            <input type='submit' value='Search'/></form>
            <div><a href=\"?mod=panel&file=keyword&action=index\">
            <img class='icon' src=\"images/gear.png\" title=\"Search Index\"/></a></div>
            <table width=\"100%\" cellspacing=0><tr><th>No</th><th>Word</th><th>Post count</th><th>Action</th></tr>";
        $i=0;
        while ($row=$db->fetch_array($result)){
            extract($row);
            $i++;
            $post_count=word_on_post($id);
            echo "<tr><td align='center'>$i</td>
            <td>$word</td><td>$post_count</td><td align='center'>
            <a href=\"?mod=panel&file=keyword&action=edit&id=$id\">
            <img class='icon' src=\"images/edit.png\" title=\"Delete Tag\"/></a>
            <a href=\"?mod=panel&file=keyword&action=delete&id=$id\">
            <img class='icon' src=\"images/delete.png\" title=\"Delete Tag\"/></a>
            </td></tr>";
        }
        echo "</table>";
        if ($p>0){
            echo "<div>Halaman : ";
        
        for ($i=0;$i<$p;$i++){
            $halaman=$i+1;
            echo "<a href=\"?mod=panel&file=keyword&page=$i\">
            [$halaman]</a> |";
        }
        echo "</div>";
        }
    }
    
}
function delete_process(){
    global $db,$id;
    $channel_name=show_word($id);
    
    $msg= "Word $channel_name on Keywords was deleted!";
    
    $db->query("delete from _keywords where id=$id");
    refresh_to("?mod=panel&file=keyword", $msg, 2);
}
function edit_word(){
    global $db,$id;
    if (!empty($id)){
    $sql="select * from _keywords where id=$id";
        $row=$db->fetch_row($sql);
        extract($row);
        
    echo "<form action=\"?mod=panel&file=keyword\" method=\"post\">    
    <input type=\"hidden\" name=\"action\" value=\"edit_process\"/>
<table cellpadding=\"4\" cellspacing=\"0\">
<tr><td>Id</td><td><input type=\"hidden\" name=\"id\" value=\"$id\"/> </td></tr>
<tr><td>Word</td><td><input type=\"text\" name=\"word\" 
    class=\"textbox\" value=\"$word\"/> </td></tr>
<tr><td>Rise</td><td><input type=\"text\" name=\"rise\" 
    class=\"textbox\" value=\"$rise\"/> </td></tr>
<tr><td></td><td><input type=\"submit\" value=\"Update\"/>
        <input type=\"button\" name=\"cancel\" 
        value=\"Cancel\" onclick=\"javascript:history.go(-1);\"/></td></tr></table></form>";
    
    }
}
function edit_process(){
    global $db,$id,$word,$rise;
    if (!empty($id)){
    $sql="update _keywords set word='$word',rise='$rise' where id=$id";
    $db->query($sql);
    $msg="Keyword berhasil di Update!";
    refresh_to("?mod=panel&file=keyword", $msg, 2);
    }
    
}
global $action,$is_admin;
if ($is_admin=='1'){
switch ($action){
    case 'index':
        keyword_index();
        break;
    case 'edit_process':
        edit_process();
        break;
    case 'edit':
        edit_word();
        break;
    case 'delete':
        delete_process();
        break;
    case 'indexing':
        indexing_process();
        break;
    default:
        word_list();
        break;
}
} else {
    refresh_to("?mod=posting", "Sorry, You are not Administrator", 1);
}
?>
<div id="progressbar"><img src="images/ajax-loader.gif" /></div>
<script>
    $('#progressbar').hide();
    $('#indexing').click(function(){
       $('#progressbar').show(); 
       $.ajax('index.php?mod=panel&file=keyword&action=indexing')
       .done (function (){
           $('#progressbar').hide();
           
       });
    });
</script>
    